package com.qzsoft.zhjd.common.importUtils;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qzsoft.jeemis.common.utils.SpringContextUtils;
import com.qzsoft.jeemis.platform.dict.service.BasicCodeService;
import com.qzsoft.jeemis.repository.sys.dao.BasicCodeitemDao;
import com.qzsoft.jeemis.repository.sys.entity.BasicCodeitemEntity;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ImportExcelUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ImportExcelUtil.class);
    private static BasicCodeitemDao sysCodeDato = (BasicCodeitemDao) SpringContextUtils.getBean(BasicCodeitemDao.class);

    public ImportExcelUtil(){}

    public static <T> List<T> importExcelMore(File file, Class<?> pojoClass, ImportParams params) {
        FileInputStream in = null;

        List var4;
        try {
            in = new FileInputStream(file);
            var4 = (new ExcelImportService()).importExcelByIs(in, pojoClass, params, false).getList();
        } catch (ExcelImportException var9) {
            throw new ExcelImportException(var9.getType(), var9);
        } catch (Exception var10) {
            LOGGER.error(var10.getMessage(), var10);
            throw new ExcelImportException(var10.getMessage(), var10);
        } finally {
            IOUtils.closeQuietly(in);
        }

        return var4;
    }

    public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, ImportParams params, boolean needMore) throws Exception {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Excel import start ,class is {}", pojoClass);
        }
        List<T> result = new ArrayList();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ExcelImportResult importResult;
        try {
            byte[] buffer = new byte[1024];

            int len;
            while((len = inputstream.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }

            baos.flush();
            InputStream userIs = new ByteArrayInputStream(baos.toByteArray());
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Excel clone success");
            }
            Workbook book = WorkbookFactory.create(userIs);
            boolean isXSSFWorkbook = !(book instanceof HSSFWorkbook);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Workbook create success");
            }
            importResult = new ExcelImportResult();
        } finally {
            IOUtils.closeQuietly(baos);
        }

        return importResult;
    }

    public static BasicCodeitemEntity getCodeId(String codeType,String codeMsg){
        QueryWrapper<BasicCodeitemEntity> query = new QueryWrapper<>();
        query.eq("description",codeMsg).eq("codeid",codeType);
        return sysCodeDato.selectOne(query);
    }
}
